** For "Who Holds Sovereign Debt and Why It Matters" by Fang, Hardy, and Lewis
clear all
set more off
cd "~/Dropbox/Sovereign Bond Project/Replication_Package_submission_RFS"

// subplot 1
use "Data/Figure1_Data/WEO_Debt_to_GDP.dta", clear 
label define yearlbl 2000 "00" 2003 "03" 2006 "06" 2009 "09" 2012 "12" 2015 "15" 2018 "18" 2021 "21"
label values Year yearlbl

twoway line Debt_to_GDP Year if Country == "Advanced economies" & (Year >= 2000) & (Year <= 2021), lpattern(solid) lwidth(thick) color("213 91 32") || ///
	   line Debt_to_GDP Year if Country == "Emerging market and developing economies" & (Year >= 2000) & (Year <= 2021), lpattern(solid) lwidth(thick) color("66 127 109") || , ///
	   legend(label(1 "AE         ") label(2 "EM & DC    ") region(lpattern(blank)) pos(6) col(2) size(large) bplace(l)) ///
	   ytitle("") xtitle("") ///
	   xlabel(2000(3)2021, labsize(large) valuelabel tp(i)) ///
	   ylabel(0(0.25)1.25, labsize(large) angle(horizontal) tp(i) format(%9.2f)) ///
	   scheme(gg_tableau) graphregion(color(white)) ///
	   name(Fig1a, replace) xsize(5.2) ysize(5)
graph export "Figures/Figure1a.pdf", replace
graph export "Figures/Figure1a.eps", replace

// subplot 2, 3	   
import excel "Data/Paper_Data/FLZ_Data.xlsx", firstrow clear
destring Year, replace
drop if mi(Total_Debt, Domestic_Total, Domestic_CentralBank, Domestic_Bank, Domestic_NonBank, Foreign_Total, Foreign_Official, Foreign_Bank, Foreign_NonBank)
do "Code/00_Country_Definition.do"

distinct Country
tab Country

// United States and China
preserve
keep if Country == "United States" | Country == "China"
gen share_dom = Domestic_Total / Total_Debt
gen share_nonbank = (Foreign_NonBank + Domestic_NonBank) / Total_Debt
keep Country Year share_dom share_nonbank
sort Country Year
tempfile indiv_country
save `indiv_country'
restore

// AE excluding United States and EM excluding China
drop if Country == "United States" | Country == "China"
drop if DC == 1
keep if Year>= 1997
drop if AE == 1 & Year < 2002

encode Country, gen(Country_id)
xtset Country_id Year
by Country_id: gen nyear=[_N]
keep if nyear == 17 & AE == 1 | nyear == 22
distinct Country if AE == 1 // 16
distinct Country if EM == 1 // 21
replace Country = "AE" if AE == 1
replace Country = "EM" if EM == 1
collapse (sum) Foreign_NonBank Domestic_NonBank Domestic_Total Total_Debt, by(Year Country)
gen share_dom = Domestic_Total / Total_Debt
gen share_nonbank = (Foreign_NonBank + Domestic_NonBank) / Total_Debt
keep Country Year share_dom share_nonbank
sort Country Year
append using `indiv_country'

label define yearlbl 1997 "97" 2000 "00" 2003 "03" 2006 "06" 2009 "09" 2012 "12" 2015 "15" 2018 "18"
label values Year yearlbl

twoway line share_dom Year if Country == "AE" & (Year >= 1997), lpattern(solid) lwidth(thick) color("173 56 54") || ///
       line share_dom Year if Country == "United States" & (Year >= 1997), lpattern(dash) lwidth(thick) color("64 112 174") || ///
	   line share_dom Year if Country == "EM" & (Year >= 1997), lpattern(solid) lwidth(thick) color("234 161 33") || ///
       line share_dom Year if Country == "China" & (Year >= 1997), lpattern(dash) lwidth(thick) color("99 61 131") ///
	   legend(order(1 2) label(1 "AE(excl US)   ") label(2 "US") region(lpattern(blank)) cols(2) pos(6) size(large) bplace(l)) ///
	   ytitle("") xtitle("") ///
	   xlabel(1997(3)2018, labsize(large) valuelabel tp(i)) ///
	   ylabel(0.0(0.2)1.0, labsize(large) angle(horizontal) tp(i) format(%9.2f)) ///
	   scheme(gg_tableau) graphregion(color(white)) /// 
	   name(Fig1b, replace) xsize(5.2) ysize(5)
graph export "Figures/Figure1b.pdf", replace
graph export "Figures/Figure1b.eps", replace

twoway line share_nonbank Year if Country == "AE" & (Year >= 1997), lpattern(solid) lwidth(thick) color("173 56 54") || ///
       line share_nonbank Year if Country == "United States" & (Year >= 1997), lpattern(dash) lwidth(thick) color("64 112 174") || ///
	   line share_nonbank Year if Country == "EM" & (Year >= 1997), lpattern(solid) lwidth(thick) color("234 161 33") || ///
       line share_nonbank Year if Country == "China" & (Year >= 1997), lpattern(dash) lwidth(thick) color("99 61 131") ///
	   legend(order(3 4) label(3 "EM(excl CN)   ") label(4 "CN") region(lpattern(blank)) cols(2) pos(6) size(large) bplace(l)) ///
	   ytitle("") xtitle("") ///
	   xlabel(1997(3)2018, labsize(large) valuelabel tp(i)) ///
	   ylabel(0.0(0.2)1.0, labsize(large) angle(horizontal) tp(i) format(%9.2f)) ///
	   scheme(gg_tableau) graphregion(color(white)) /// 
	   name(Fig1c, replace) xsize(5.2) ysize(5)
graph export "Figures/Figure1c.pdf", replace	   
graph export "Figures/Figure1c.eps", replace

